<?php

/**
 * 
 * @author Administrator
 *
 */
class AchievementController extends AdminBaseController {
	
	public function index(){
		$this->setTitle('成就列表', '数据需从Excel导入');
	
		$filter = new PubFilter();
		$grid   = new PubGrid();

        $filter->addFilter('a_id', 'input', '成就标识')
               ->addFilter('achievement_name', 'input', '成就名称');
		//$filter->addExport();
		$url = PubFunc::buildUrl("/oss/?r=achievement/importGoods");
		$filter->addImport('成就导入',$url);
		
		$params = $filter->get();
		$header = AchievementService::indexHeader();
		if(PubFunc::isAjax()){
			$result = AchievementService::indexData($params);
			
			$this->out = array(
					'count'     => $result['count'],
					'data'      => $result['data'],
					'result'    => true
			);
			$this->outType = 'json';
		}else{
			$order  = 'id';
			$params['m'] = PubRequest::R('m');
			$url = PubFunc::ajaxUrl("?r=achievement/index", $params);
			$grid->addGrid($header, $url, array(
					'name'=>'数据',
					'order'=>array($order, 'asc'),
					'type'=>'achievement/index',
			));
			
			$this->out['grid'] = $grid->show();
			$this->out['filter'] = $filter->show();
			
			$this->out['downUrl'] = '/public/template/achievement.xlsx';
			$user = AccAuth::me();
			if($user['IsAdmin'] == 1 || $user['Identity'] == 2){
				$this->out['table'] = 'achievement';
			}
		}
	}
	
	public function delete(){
		$id = $this->R('id');
	
		$result = AchievementService::delete($id);
	
		if($result !== false){
			$this->showMessage('删除成功', PubFunc::buildUrl("/oss/?r=achievement/index"));
		}else{
			$this->showMessage('删除失败');
		}
	}
	
	public function importGoods(){
		$upload = AchievementService::upload($_FILES);
		if($upload["status"] == 1){ //上传成功
			$filePath = $upload['data'];
			$data = LibExcel::import($filePath);
			$result = AchievementService::xlsAdd($data);
			$out = $result;
		}else{
			$out = $upload; //上传失败
		}
		
		$this->out = $out;
		$this->outType = 'json';
	}

    

    

    
}